﻿using System.Windows.Forms;
using System.Drawing;
using System;
using Mota.EditModule;
namespace Mota.XMLControls
{
    public class DXGuiDictionaryDecorator:GuiDictionaryDecorator
    {
        public DXGuiDictionaryDecorator(GuiControl decoratedControl)
        {
            visualControl = new UserControl(){
                Dock = DockStyle.Top,
                Height = (decoratedControl.visualControl as Control).Height+2
            };
            this.decoratedControl = decoratedControl;
            this.decoratedControl.Dock = DockStyle.Fill;
            Size sz = new Size((decoratedControl.visualControl as Control).Height, (decoratedControl.visualControl as Control).Height);
           
            if(decoratedControl.IsSimple)
            {

                Button btn = new Button()
                {
                    Text = "",
                    Dock = DockStyle.Right,
                    Size = sz,
                    FlatStyle = FlatStyle.Flat,
                    Image = new Bitmap(Resources.GetImage("181954D0-5F08-4F7A-82C0-B3E609B8B736"))
                };
                (visualControl as UserControl).Controls.AddRange(new Control[] { this.decoratedControl.visualControl as Control, btn });
                btn.Click += new System.EventHandler(btnDictClick);
            }
            else
            {
                ToolStrip _ts = new ToolStrip();
                (visualControl as UserControl).Controls.Add(_ts);
                //TODO:modify icons
                ToolStripButton btnDict = new ToolStripButton();
                (btnDict as ToolStripButton).Image = new Bitmap(Resources.GetImage("181954D0-5F08-4F7A-82C0-B3E609B8B736"));

                _ts.Items.AddRange(new ToolStripItem[] { btnDict });

                (visualControl as UserControl).Controls.AddRange(new Control[] { this.decoratedControl.visualControl as Control, _ts });
                btnDict.Click += new System.EventHandler(btnDictClick);
            }
            
            
        }

        private void btnDictClick(object sender, EventArgs e)
        {
            // Check if source name exists
            if (decoratedControl.XmlAttributes.ContainsKey("NameOrig"))
                // pass the source CIPHER to infoModule
                this.CustomEventCall(new DictClickEventArgs(decoratedControl.XmlAttributes["NameOrig"]));
        }
        
        public override void Add(GuiControl guiControl)
        {
            base.Add(guiControl);
            decoratedControl.Add(guiControl);
        }


        public override string Text
        {
            get
            {
                return decoratedControl.Text;
            }
            set
            {
                decoratedControl.Text = value;
            }

        }

        public override object Binding
        {
            get
            {
                return decoratedControl.Binding;
            }
            set
            {
                decoratedControl.Binding = value;
            }
        }
        public override void SetValue(object valueBinding)
        {
            decoratedControl.SetValue(valueBinding);
        }


        public override object GetValue()
        {
            return decoratedControl.GetValue();
        }

        public override bool UseOnlyActive
        {
            get
            {

                return (decoratedControl as IDatasourceObject).UseOnlyActive;
            }
            set
            {
                (decoratedControl as IDatasourceObject).UseOnlyActive = value;
            }
        }

        public override bool UseGroupBy
        {
            get
            {

                return (decoratedControl as IDatasourceObject).UseGroupBy;
            }
            set
            {
                (decoratedControl as IDatasourceObject).UseGroupBy = value;
            }
        }

        public override bool IsEditable
        {
            get
            {

                return (decoratedControl as IDatasourceObject).IsEditable;
            }
            set
            {
                (decoratedControl as IDatasourceObject).IsEditable = value;
            }
        }

        public override void Refresh(ConnectionConfigs configs, BindingSource mainBs)
        {
            (decoratedControl as IDatasourceObject).Refresh(configs, mainBs);
        }
    }
}
